package ink.yql.service.Impl;

import ink.yql.dao.AttentionDao;
import ink.yql.dao.UserDao;
import ink.yql.model.Attention;
import ink.yql.model.User;
import ink.yql.service.AttentionService;
import ink.yql.util.RestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class AttentionServiceImpl implements AttentionService {
    @Autowired
    private AttentionDao attentionDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private HttpServletRequest req;

    @Override
    public RestObject getByUid() {
        // 获取session中的用户
        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            return RestObject.builder().code("404").message("请先登录").build();
        }
        // 获取关注的集合
        List<Attention> list = attentionDao.getAttentionByUid(user.getUid());
        List<User> users = new ArrayList<>();
        list.forEach(att -> {
            users.add(userDao.getByUid(att.getOtherid()));
        });
        return RestObject.builder().code("200").message("查询关注用户成功").data(users).build();
    }

    @Override
    public RestObject getByOtherid(Integer otherid) {
        // 校验数据格式
        if (otherid != null) {
            return RestObject.builder().code("400").message("请传输正确的数据").build();
        }
        // 查询到所有对当前用户关注的用户
        List<Attention> list = attentionDao.getAttentionByOtherid(otherid);
        List<User> users = new ArrayList<>();
        // 获取具体的用户
        list.forEach(att -> {
            users.add(userDao.getByUid(att.getUid()));
        });
        return RestObject.builder().code("200").message("查询被关注用户成功").data(users).build();
    }

    @Override
    public RestObject addOne(Integer otherid) {
        // 获取session中的用户
        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            return RestObject.builder().code("404").message("请先登录").build();
        }
        // 校验数据格式
        if (otherid == null) {
            return RestObject.builder().code("400").message("请传输正确的数据").build();
        }
        User otherUser = userDao.getByUid(otherid);
        if (otherUser == null) {
            return RestObject.builder().code("400").message("关注的用户不存在").build();
        }
        Attention attention = Attention.builder().uid(user.getUid()).otherid(otherid).build();
        Attention whether = attentionDao.whetherToFollow(attention);
        if (whether == null) {
            userDao.updateUfansCount(attention.getOtherid(),1);
            userDao.updateUattentionCount(attention.getUid(),1);
            attentionDao.addOne(attention);
            return RestObject.builder().code("200").message("关注成功").build();
        }
        userDao.updateUfansCount(attention.getOtherid(),-1);
        userDao.updateUattentionCount(attention.getUid(),-1);
        attentionDao.delete(whether);
        return RestObject.builder().code("200").message("取消关注成功").build();
    }
}
